package com.xuetang9.t9.hotel.mapper.sql.impl;
import com.xuetang9.t9.hotel.mapper.condition.ICondition;
import com.xuetang9.t9.hotel.mapper.condition.impl.CommodityTypeCondition;
import com.xuetang9.t9.hotel.mapper.sql.IMapperSql;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class CommodityTypeMapperSql implements IMapperSql {
    @Override
    public String selectByAll(Map map) {
        ICondition key = (ICondition) map.get("key");
        return getCondition(key,"*",true);
    }

    @Override
    public String selectByCount(Map map) {
        ICondition key = (ICondition) map.get("key");
        return getCondition(key,"COUNT(*)",false);
    }

    private String getCondition(ICondition condition, String select,boolean isPage) {
        SQL sql = new SQL();
        sql.SELECT(select).FROM("g_commoditytype");
        if(condition == null){
            return sql.toString();
        }
        CommodityTypeCondition key = null;
        if(condition instanceof CommodityTypeCondition){
            key = (CommodityTypeCondition) condition;
        }else{
            throw  new RuntimeException("condition type Don't match");
        }
        if(null != key){
            if(null != key.getName() && !key.getName().isEmpty()){
                sql.WHERE("commeype_name LIKE '%"+key.getName()+"%'");
            }
            if(key.judge()  && isPage){
                sql.LIMIT(key.getCurrPage()+","+key.getPageSize());
            }
        }
        return sql.toString();
    }
}
